package cn.pigeon.health.controller;

import cn.pigeon.health.constant.MessageConstant;
import cn.pigeon.health.entity.Result;
import cn.pigeon.health.pojo.OrderSetting;
import cn.pigeon.health.service.OrderSettingService;
import cn.pigeon.health.utils.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约管理控制器
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传Excel文件批量导入
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            //读取Excel文件
            List<String[]> lstData = POIUtils.readExcel(excelFile);
            //注意：MultipartFile excelFile不能作为参数直接传递,否则,dubbo调用是不会成功的
            //MultipartFile不能作为参数，通过分布式调用接口直接上传，因为是final 无法实现序列化；
            orderSettingService.importData(lstData);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 查询预约管理数据(按月查)
     * @param map
     * @return
     */
    @RequestMapping("/queryData")
    public Result queryData(String startDate , String endDate){
        try {
            Map<String,String> map = new HashMap<String,String>();
            map.put("startDate",startDate);
            map.put("endDate",endDate);
            List<Map> lstData = orderSettingService.queryData(map);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,lstData);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 设置可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
